package org.example.pack1210.dao;

import lombok.SneakyThrows;
import org.example.pack1210.bean.Emp;
import org.example.pack1210.db.DBManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

// 多表联查
public class Emp2DAO {

    private Connection conn;
    private PreparedStatement ps;
    private ResultSet rs;

    @SneakyThrows
    // 方案一：这里只查询员工信息
    public Emp findById(int id) {
        conn = DBManager.getConnection();
        String sql = "select * from emp where state=1 and empno=" + id;
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
        if (rs.next()) {
            Emp emp = new Emp();
            int empNo = rs.getInt(1);
            String enname = rs.getString(2);
            String job = rs.getString("job");
            int mgr = rs.getInt(4);
            String hireDate = rs.getString("hireDate");
            double sal = rs.getDouble("sal");
            double comm = rs.getDouble("comm");
            int deptNo = rs.getInt("deptno");
            int state = rs.getInt(9);
            // 封装
            emp.setEmpNo(empNo);
            emp.setComm(comm);
            emp.setJob(job);
            emp.setEnname(enname);
            emp.setHiredate(hireDate);
            emp.setDeptNo(deptNo);
            emp.setMgr(mgr);
            emp.setState(state);
            emp.setSal(sal);
            return emp;
        }
        return null;
    }
}